package com.fingard.dsp.bank.directbank.vbao02.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class SignUtil {
	
	/**
	 * 获取签名
	 */
	public static String getDigest(String src) {
		try {
			return CryptTool.md5Digest(src).toUpperCase();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	/**
	 * 校验签名
	 * @param src
	 * @param digestStr
	 * @return
	 */
	public static boolean verifyDigest(String src, String digestStr) {
		String srcDigestStr = SignUtil.getDigest(src);
		System.out.println("本机签名:" + srcDigestStr);
		if (srcDigestStr.equals(digestStr)) {
			return true;
		}
		return false;
	}

	/**
	 * 校验签名
	 * @param params
	 * @return
	 */
    public static String getSignCheckContentV2(Map<String, String> params) {
        if (params == null) {
            return null;
        }

        params.remove("sign");

        StringBuffer content = new StringBuffer();
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);

        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);
            content.append((i == 0 ? "" : "&") + key + "=" + value);
        }

        return content.toString();
    }
	
	  /**
     * 签名拼装
     * @param sortedParams
     * @return
     */
    public static String getSignContent(Map<String, Object> sortedParams) {
        StringBuffer content = new StringBuffer();
        List<String> keys = new ArrayList<String>(sortedParams.keySet());
        Collections.sort(keys);
        int index = 0;
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = sortedParams.get(key) + "";
            if (StringUtilsForSign.areNotEmpty(key, value)) {
                content.append((index == 0 ? "" : "&") + key + "=" + value);
                index++;
            }
        }
        return content.toString();
    }
	
}
